﻿using Apache.NMS.ActiveMQ.Commands;
using LD.Common;
using log4net.Core;
using System;
using System.ComponentModel;
using System.Drawing;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using WeifenLuo.WinFormsUI.Docking;
namespace LD.Host
{
    public partial class FrmLog : DockContent
    {
        private static FrmLog instance;
        private static object obj = new object();
        private readonly string id=Guid.NewGuid().ToString();
        public FrmLog()
        {
            InitializeComponent();
            this.Load += FrmLog_Load;

            //this.lv_msg.Columns[0].Width = this.lv_msg.Width - 100;
            this.FormClosing += FrmLog_FormClosing;
        }

        private void FrmLog_Shown(object sender, EventArgs e)
        {
            PubSub.Hub.Default.Subscribe<LanguageType>(id,(p) => ChangeLanguage(p));
            ChangeLanguage(LanguageType.中文);
        }
        private void ChangeLanguage(LanguageType type)
        {
            this.Text = Language.GetString("Logs");
        }
        public static FrmLog Instance
        {
            get
            {
                if (instance == null)
                {
                    lock (obj)
                    {
                        if (instance == null)
                            instance = new FrmLog();
                    }
                }
                return instance;
            }
        }
        private void FrmLog_Load(object sender, EventArgs e)
        {
            Log4.LogEvent += LogHelper_EventLog;
            Log4.Info("程式启动完成");
        }

        private void FrmLog_FormClosing(object sender, FormClosingEventArgs e)
        {
            PubSub.Hub.Default.Unsubscribe<LanguageType>(id);
            Log4.LogEvent -= LogHelper_EventLog;
        }
        public void LogHelper_EventLog(string message, LogLevel level)
        {
            //Task.Run(new Action(() =>
            //{
            Color color = Color.Black;
            switch (level)
            {
                case LogLevel.一般:
                    break;
                case LogLevel.警告:
                    color = Color.DarkRed;
                    break;
                case LogLevel.严重:
                    color = Color.Red;
                    break;
                default:
                    break;
            }
            ListViewItem item = new ListViewItem();
            item.ForeColor = color;
            item.Text = message;

            //if (this.lv_msg == null)
            //    return;
            if (!this.IsDisposed)
            {
                this.lv_msg.BeginInvoke(new Action(() =>
                {
                    lv_msg.BeginUpdate();

                    lv_msg.Items.Insert(0, item);

                    if (lv_msg.Items.Count > 100)
                    {
                        lv_msg.Items.RemoveAt(lv_msg.Items.Count - 1);
                    }
                    lv_msg.EndUpdate();

                    this.lv_msg.Columns[0].Width = -1;

                    lv_msg.Refresh();

                }));
            }

            //}));
        }

        private void 全部清除ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.lv_msg.Items.Clear();
        }

        private void 复制所选ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            var listViewItem = this.lv_msg.SelectedItems;
            StringBuilder sb = new StringBuilder();
            foreach (ListViewItem item in listViewItem)
            {
                sb.Append(item.Text);
                sb.Append("\r\n");
            }
            if (sb.Length > 0)
                Clipboard.SetText(sb.ToString());
            sb.Clear();
        }

        private void contextMenuStrip1_Opening(object sender, CancelEventArgs e)
        {
            if (this.lv_msg.SelectedItems.Count > 0)
                复制所选ToolStripMenuItem.Visible = true;
            else
                复制所选ToolStripMenuItem.Visible = false;
        }
    }
}
